<?php
require '../utils/conn.php';
include '../models/Tag.php';

function getAllTags() {
	$query = "Select * from question_tag where super_id=0";
	$result = mysql_query($query);
	$tags = array();
	while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
	{
		$tag = new Tag();
		$tag->setTagId($row['tag_id']);
		$tag->setName($row['name']);
		$tag->setSuperId($row['super_id']);
		$subQuery = "select * from question_tag where super_id=".$tag->tagId;
		$subResult = mysql_query($subQuery);
		$subTags = array();
		while ($subRow = mysql_fetch_array($subResult,MYSQL_ASSOC))
		{
			$subTag = new Tag();
			$subTag->setTagId($subRow['tag_id']);
			$subTag->setName($subRow['name']);
			$subTag->setSuperId($subRow['super_id']);
			$subTags[] = $subTag;
		}
		$tag->setSubTags($subTags);
		$tags[] = $tag;
	}
	return $tags;
}

function addTag($tagName, $superTag) {
	$query = "SELECT * from question_tag where name='$tagName' and super_id=$superTag";
	echo $query;
	$result = mysql_query($query);
	if ($myrow = mysql_fetch_array($result)) {
		echo "error:该标签已存在！";
		return;
	}
	$query = "INSERT INTO question_tag(name, super_id) VALUES('$tagName', $superTag)";
	$result = mysql_query($query) or die("添加标签错误：". mysql_error());
}

function deleteTag($tagId) {
	$deleteSubQuery = "DELETE FROM question_tag WHERE super_id = $tagId";
	$result = mysql_query($deleteSubQuery);
	$deleteQuery = "DELETE FROM question_tag WHERE tag_id = $tagId";
	$result = mysql_query($deleteQuery);
	
}

?>